package com.xinghai.system.service.impl;

import java.util.List;
import com.xinghai.common.utils.DateUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import com.xinghai.system.mapper.JsBannerMapper;
import com.xinghai.system.domain.JsBanner;
import com.xinghai.system.service.IJsBannerService;

/**
 * 轮播图Service业务层处理
 * 
 * @author xinghai
 * @date 2024-08-31
 */
@Service
public class JsBannerServiceImpl implements IJsBannerService 
{
    @Autowired
    private JsBannerMapper jsBannerMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询轮播图
     * 
     * @param id 轮播图主键
     * @return 轮播图
     */
    @Override
    public JsBanner getInfo(Long id)
    {
        return jsBannerMapper.getInfo(id);
    }

    /**
     * 查询轮播图列表
     * 
     * @param jsBanner 轮播图
     * @return 轮播图
     */
    @Override
    public List<JsBanner> list(JsBanner jsBanner)
    {
            jsBanner.setDelFlag("0");
        return jsBannerMapper.list(jsBanner);
    }

    /**
     * 新增轮播图
     * 
     * @param jsBanner 轮播图
     * @return 结果
     */
    @Override
    public int insert(JsBanner jsBanner)
    {
        jsBanner.setCreateTime(DateUtils.getNowDate());
        return jsBannerMapper.insert(jsBanner);
    }
    /**
     * 批量新增轮播图
     *
     * @param jsBannerList 轮播图
     * @return 结果
     */
    @Override
    public int insertBatch(List<JsBanner> jsBannerList)
    {
        return jsBannerMapper.insertBatch(jsBannerList);
    }
    /**
     * 修改轮播图
     * 
     * @param jsBanner 轮播图
     * @return 结果
     */
    @Override
    public int update(JsBanner jsBanner)
    {
        jsBanner.setUpdateTime(DateUtils.getNowDate());
        return jsBannerMapper.update(jsBanner);
    }

    /**
     * 批量删除轮播图
     * 
     * @param ids 需要删除的轮播图主键
     * @return 结果
     */
    @Override
    public int deleteBatch(Long[] ids)
    {
        return jsBannerMapper.deleteBatch(ids);
    }

    /**
     * 删除轮播图信息
     * 
     * @param id 轮播图主键
     * @return 结果
     */
    @Override
    public int delete(Long id)
    {
        return jsBannerMapper.delete(id);
    }

    /**
     * 导入轮播图信息
     *
     * @param list 轮播图信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<JsBanner> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            JsBannerMapper jsBannerMapper = sqlSession.getMapper(JsBannerMapper.class);
            list.stream().forEach(item -> jsBannerMapper.insert(item));
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }
}
